﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Secured_ExpenseHead : System.Web.UI.Page
{
    SqlConnection conn;
    SqlCommand cmd;
    SqlDataAdapter da;
    System.Data.DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connStr"].ConnectionString);
        if (conn.State != System.Data.ConnectionState.Open)
        {
            try
            {
                conn.Open();
                FillExpenseHeads();
            }
            catch (SqlException ex)
            {
                ltMsg.Text = ex.Message;
            }
        }
    }
    private void FillExpenseHeads()
    {
        cmd = new SqlCommand("Select HeadID, ExpenseHead from HAT_ExpenseHead order by ExpenseHead asc", conn);
        ds = new System.Data.DataSet();
        da = new SqlDataAdapter(cmd.CommandText, conn);
        try
        {
            da.Fill(ds, "NewTable");
            grdExpenses.DataSource = ds.Tables["NewTable"].DefaultView;
            grdExpenses.DataBind();
        }
        catch (SqlException ex)
        {
            ltMsg.Text = ex.Message;
        }
    }
    protected void btnAddHead_Click(object sender, EventArgs e)
    {
        cmd = new SqlCommand("insert into HAT_ExpenseHead(ExpenseHead) Values(@Head)", conn);
        cmd.Parameters.Add("@Head", System.Data.SqlDbType.VarChar).Value = txtNewHead.Text.Trim();
        try
        {
            cmd.ExecuteNonQuery();
            txtNewHead.Text = string.Empty;
            FillExpenseHeads();
        }
        catch (SqlException ex)
        {
            ltMsg.Text = ex.Message;
        }
    }
}